package com.ztjava.auth.listener;

import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.kit.Ret;
import com.ztjava.auth.model.AuthUser;
import com.ztjava.auth.service.AuthService;
import com.ztjava.core.bean.sub.PushBean;
import com.ztjava.plugin.event.EventKit;
import com.ztjava.plugin.event.EventTag;
import com.ztjava.plugin.event.core.ApplicationListener;
import com.ztjava.plugin.event.core.Listener;
import com.ztjava.plugin.event.core.RequestEvent;

@Listener(tag=EventTag.AUTH_DEPT_PUSH,enableAsync=true)
public class DeptPushLisitener implements ApplicationListener<RequestEvent>{
	private static AuthService authService = Duang.duang(AuthService.class);
	
	@Override
	public Ret onApplicationEvent(RequestEvent event) {
		PushBean pushBean = (PushBean)event.getSource();
		String deptId = pushBean.getTo();
		List<AuthUser> list = authService.getDeptUserList(Integer.parseInt(deptId));
		String to = "";
		for(int i=0;i<list.size();i++){
			to = to + list.get(i).getInt("id");
			if(i<(list.size()-1)){
				to = to + ",";
			}
		}
		if(!to.equals("")){
			pushBean.setTo(to);
			EventKit.postRemote(EventTag.COMMON_PUSH_ASYNC, new RequestEvent(pushBean));
		}
		return null;
	}

}
